<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="活动名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="活动时间" prop="hdStart">-->
      <!--        <el-date-picker-->
      <!--          v-model="queryParams.hdStart"-->
      <!--          clearable-->
      <!--          type="date"-->
      <!--          value-format="YYYY-MM-DD"-->
      <!--          placeholder="请选择活动时间"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="酒店可预订时间" prop="hdStart">-->
      <!--        <el-date-picker-->
      <!--          v-model="queryParams.hdStart"-->
      <!--          clearable-->
      <!--          type="date"-->
      <!--          value-format="YYYY-MM-DD"-->
      <!--          placeholder="请选择活动开始时间"-->
      <!--        />-->
      <!--      </el-form-item>-->


      <el-form-item label="活动地点" prop="regionId">
        <el-cascader
          v-model="queryParams.regionId"
          :options="regionsList"
          :props="{ label:'text',checkStrictly:true}"
          clearable
          @change="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable @change="handleQuery">
          <el-option label="未上架" value="0" />
          <el-option label="已上架" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="Edit"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="Delete"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>
    <el-table
      v-loading="loading" :data="matchList" stripe row-key="id" :expand-row-keys="[]"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="index" width="55" label="序号" align="center" />
      <el-table-column label="活动图片" align="center" prop="photos" min-width="100">
        <template #default="scope">
          <el-image style="width: 60px;height: 45px" :src="scope.row.photos.split(',')[0]" :preview-src-list="scope.row.photos.split(',')" />
        </template>
      </el-table-column>
      <el-table-column label="活动名称" align="center" prop="name" min-width="200" :show-overflow-tooltip="true" />
      <el-table-column label="活动地点" align="center" prop="addName" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="活动时间" align="center" prop="hdTime" min-width="190">
        <template #default="scope">
          {{ parseTime(scope.row.hdStart, '{y}-{m}-{d}') }} ~ {{ parseTime(scope.row.hdEnd, '{y}-{m}-{d}') }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template #default="scope">
          {{ scope.row.status=='1'?'已上架':'未上架' }}
        </template>
      </el-table-column>
      <el-table-column label="二维码" align="center" width="80">
        <template #default="scope">
          <el-image :src="qr" style="width: 30px;height: 30px;cursor: pointer" @click.stop="getQrcode(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width" width="250">
        <template #default="scope">
          <el-button v-if="scope.row.status=='0'" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.status=='1'" type="text" @click="handleView(scope.row)">查看</el-button>
          <el-button v-if="scope.row.status=='0'" type="text" @click="handleStatus(scope.row)">上架</el-button>
          <el-button v-if="scope.row.status=='1'" type="text" @click="handleStatus(scope.row)">下架</el-button>
          <el-button v-if="scope.row.status=='0'" type="text" @click="handleDelete(scope.row)">删除</el-button>
          <el-button type="text" @click="handleLogistics(scope.row.id)">后勤管理</el-button>
          <el-button type="text" @click="handleOrder(scope.row.id)">订单管理</el-button>
          <el-button type="text" @click="handleInvoice(scope.row.id)">开票管理</el-button>
          <el-button type="text" @click="handleRecord(scope.row.id)">数据统计</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />

    <modify-match ref="modifyMatchRef" @submit="getList" />
    <el-dialog v-model="qrcodeOpen" title="查看小程序二维码" width="500px">
      <el-image :src="qrcode" style="margin: auto;display: block; width: 450px;height: 450px ">
        <template #error>
          <div class="image-slot">Loading<span class="dot">...</span></div>
        </template>
      </el-image>
    </el-dialog>
  </div>
</template>

<script setup name="Logistics_Match">

import { reactive } from '@vue/runtime-core'
import { getCurrentInstance, ref, toRefs } from 'vue'
import ModifyMatch from './modify'
import {
  changeStatus,
  delActivity,
  getActivity,
  getMiniappQrCode,
  listActivity
} from '@/api/logex/activity'
import useUserStore from '@/store/modules/user'
import { computed } from '@vue/reactivity'
import qr from '@/assets/icons/svg/qrcode.svg'
import { useRouter } from 'vue-router'


const { proxy } = getCurrentInstance()
const router = useRouter()

const matchList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const qrcodeOpen = ref(false)
const qrcode = ref('')

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    regionId: undefined,
    province: undefined,
    city: undefined,
    area: undefined,
    status: undefined
  }
})

const { queryParams } = toRefs(data)
const regionsList = computed(() => useUserStore().regionsList)

function getList() {
  loading.value = true

  if (queryParams.value.regionId) {
    queryParams.value.province = queryParams.value.regionId[0]
    queryParams.value.city = queryParams.value.regionId[1]
    queryParams.value.area = queryParams.value.regionId[2]
  } else {
    queryParams.value.province = null
    queryParams.value.city = null
    queryParams.value.area = null
  }

  listActivity(queryParams.value).then(response => {
    matchList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

//#region 页面按钮
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs['modifyMatchRef'].open({
    title: '添加赛事信息'
  })
}
//#endregion

//#region 赛事 table 按钮
/**
 * 多选框
 * @param selection
 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 修改按钮操作 */
function handleUpdate(row) {
  proxy.$modal.loading()
  getActivity(row.id).then(response => {
    proxy.$modal.closeLoading()
    proxy.$refs['modifyMatchRef'].open({
      title: '修改赛事信息',
      match: response.data
    })
  })
}

function handleView(row) {
  proxy.$modal.loading()
  getActivity(row.id).then(response => {
    proxy.$modal.closeLoading()
    proxy.$refs['modifyMatchRef'].open({
      title: '查看赛事信息',
      match: response.data,
      isView: true
    })
  })
}

function handleStatus(row) {
  changeStatus({
    id: row.id,
    status: row.status == '0' ? '1' : '0'
  }).then(getList)
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除"' + row.name + '"的数据项？').then(function() {
    loading.value = true
    return delActivity(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess('删除成功')
  }).finally(() => {
    loading.value = false
  })
}

function handleLogistics(matchId) {
  router.push('/logistics/match/manage/' + matchId)
}

function handleOrder(matchId) {
  router.push('/logistics/match/order/' + matchId)
}

function handleInvoice(matchId) {
  router.push('/logistics/match/invoice/' + matchId)
}

function handleRecord(matchId) {
  router.push('/logistics/match/record/' + matchId)
}

function getQrcode(row) {
  qrcodeOpen.value = true
  qrcode.value = ''
  getMiniappQrCode(row.id)
    .then(res => {
      qrcode.value = 'data:image/gif;base64,' + res.data
    })
}
//#endregion

getList()

</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: var(--el-fill-color-light);
  color: var(--el-text-color-secondary);
  font-size: 14px;
}
.dot {
  animation: dot 2s infinite steps(3, start);
  overflow: hidden;
}
</style>
